import 'package:flutter/material.dart';
import 'package:flutter_deer/ui/cashier/provider/common_http_provider.dart';
import 'package:flutter_deer/util/input_formatter/mny_format.dart';
import 'package:get/get.dart';

class SupplyOilDialog extends StatefulWidget {
  const SupplyOilDialog({super.key});

  @override
  State<SupplyOilDialog> createState() => _SupplyOilDialogState();
}

class _SupplyOilDialogState extends State<SupplyOilDialog> {
  final TextEditingController _usernameController = TextEditingController();
  final TextEditingController _passwordController = TextEditingController();
  final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
  final commonHttpProvider = Get.find<CommonHttpProvider>();

  @override
  void dispose() {
    _usernameController.dispose();
    _passwordController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return AlertDialog(
      title: const Text('补单'),
      content: Form(
        key: _formKey,
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            TextFormField(
              controller: _usernameController,
              decoration: const InputDecoration(
                labelText: '枪号',
              ),
              validator: (value) {
                if (value?.isEmpty ?? true) {
                  return '请输入枪号';
                }
                return null;
              },
            ),
            const SizedBox(height: 16),
            TextFormField(
              controller: _passwordController,
              decoration: const InputDecoration(
                labelText: '金额',
              ),
              validator: (value) {
                if (value?.isEmpty ?? true) {
                  return '请输入金额';
                }
                return null;
              },
            ),
          ],
        ),
      ),
      actions: [
        TextButton(
          onPressed: () {
            Navigator.of(context).pop();
          },
          child: const Text('取消'),
        ),
        ElevatedButton(
          onPressed: () {
            if (_formKey.currentState?.validate() ?? false) {
              commonHttpProvider.supplyOil(
                orderNo: int.parse(_usernameController.text),
                amount: _passwordController.text.toFen,
                success: () {
                  Navigator.of(context).pop();
                },
              );
            }
          },
          child: const Text('确认'),
        ),
      ],
    );
  }
}

// 使用示例
void showSupplyOilDialog(BuildContext context) {
  showDialog<void>(
    context: context,
    builder: (BuildContext context) {
      return const SupplyOilDialog();
    },
  );
}
